jDev = {
  
  _modules: [
    'event',
    'console',
    'win'
  ],

  basePath: null,
  sourcePath: null,
  importPath: null,

  isFirefox:  navigator.userAgent.indexOf('Firefox') != -1,
  isIE:       navigator.userAgent.indexOf('MSIE') != -1,
  isOpera:    navigator.userAgent.indexOf('Opera') != -1,
  isSafari:   navigator.userAgent.indexOf('AppleWebKit') != -1,
  
  init: function() {
    jDev.findLocation();
    
    if (!jDev.isFirefox) {
      jDev.loadScript(jDev.importPath + 'firebugLite/firebug-lite-web.js');
    }
    
    jDev.loadCSS(jDev.sourcePath + 'jproton.dev.css');
    for(var i=0, m=jDev._modules, mi; mi=m[i]; i++) {
      jDev.loadScript(jDev.sourcePath + 'modules/dev/' + mi + '.js');
    }
  },
  
  findLocation: function() {
    var rePath = /^(.*\/)[^\/]+\.\w+.*$/;
    var reProtocol = /^\w+:\/\//;
    var head = document.documentElement.firstChild;
    var path = null;
    
    for(var i=0, c=head.childNodes, ci; ci=c[i]; i++) {
      if ( ci.nodeName == 'SCRIPT' && 
          (ci.src.indexOf('jproton.js') != -1 || ci.attributes.jproton) ) {
        
        if (reProtocol.test(ci.src)) {
          // absolute path
          path = rePath.exec(ci.src)[1];
          
        } else {
          // relative path
          var r = rePath.exec(ci.src);
          var src = r ? r[1] : ci.src;
          var rel = /^((\.\.\/)+)(.*)/.exec(src);
          var lastFolder = /^(.*\/)\w+\/$/;
          path = rePath.exec(location.href)[1];
          
          if (rel) {
            var j = rel[1].length/3;
            var p;
            while (j-- > 0) {
              path = lastFolder.exec(path)[1];
            }
            path += rel[3];
          }
        }
        
        break;
      }
    }
    
    if (path && /source\/$/.test(path)) {
      jDev.sourcePath = path;
      jDev.basePath = path.substr(0, path.length-7);
      jDev.importPath = jDev.basePath + 'import/';
    } else {
      throw 'Não foi possível encontrar o caminho automaticamente!';
    }
  },
  
  /*
   * Carrega o script dinamicamente.
   */
  loadScript: function(url) {
    var agent = navigator.userAgent;

    if (jDev.isIE || jDev.isSafari) {
      document.write('<scr'+'ipt src="' + url + '"><\/scr'+'ipt>');
       
    } else {
      var script = document.createElement('script');
      script.src = url;
      document.documentElement.firstChild.appendChild(script);
    }
  },
  
  loadCSS: function(url){
    var link = document.createElement('link');
    link.href = url;
    link.rel = 'stylesheet';
    link.type = 'text/css';
    document.documentElement.firstChild.appendChild(link);
  },
  
  getCSS: function(e, name) {
    return window.getComputedStyle ? // Tenta o modo Mozilla
        document.defaultView.getComputedStyle(e,null)[name] || 
        e.style[name] :          // Ou então, tenta a propriedade style
        e.currentStyle ?         // Tenta o modo IE
        e.currentStyle[name] ||  // Se não der certo,
        e.style[name] :          // tenta a propriedade style. Senão,
        undefined;               // Não foi possível recuperar o estilo
  },
  
  
  /*
   * From Jesse Skinner:
   * http://www.thefutureoftheweb.com/blog/adddomloadevent
   * 
   * (c)2006 Jesse Skinner/Dean Edwards/Matthias Miller/John Resig
   * Special thanks to Dan Webb's domready.js Prototype extension
   * and Simon Willison's addLoadEvent
   */
  onReady: (function(){
    // create event function stack
    var load_events = [],
      load_timer,
      script,
      done,
      exec,
      old_onload,
      init = function () {
        done = true;
  
        // kill the timer
        clearInterval(load_timer);
  
        // execute each function in the stack in the order they were added
        while (exec = load_events.shift())
          exec();
  
        if (script) script.onreadystatechange = '';
      };
  
    return function (func) {
      // if the init function was already ran, just run this function now and stop
      if (done) return func();
  
      if (!load_events[0]) {
        // for Mozilla/Opera9
        if (document.addEventListener)
          document.addEventListener('DOMContentLoaded', init, false);
  
        // for Internet Explorer
        /*@cc_on @*/
        /*@if (@_win32)
          document.write('<script id=__ie_onload defer src=//0><\/scr'+'ipt>');
          script = document.getElementById('__ie_onload');
          script.onreadystatechange = function() {
            if (this.readyState == 'complete')
              init(); // call the onload handler
          };
        /*@end @*/
  
        // for Safari
        if (/WebKit/i.test(navigator.userAgent)) { // sniff
          load_timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState))
              init(); // call the onload handler
          }, 10);
        }
  
        // for other browsers set the window.onload, but also execute the old window.onload
        old_onload = window.onload;
        window.onload = function() {
          init();
          if (old_onload) old_onload();
        };
      }
  
      load_events.push(func);
    };
  })()
};

jDev.init();
